package controller.Operation;

import util.BookDao;
import util.NormalUserDao;

import javax.swing.*;
import javax.swing.table.DefaultTableModel;

/**
 * @Question Delete
 * @Date 2022/12/18 12:29
 * @Solution
 */
public class Delete {
    public static int deleteBook(DefaultTableModel bookModel, JTable bookTable) {
        //获取选中行
        int row = bookTable.getSelectedRow();
        if(row==-1) {
            JOptionPane.showMessageDialog(null, "未选择需要删除的行!");
            return -1;
        }
        //根据选中行id在数据库中删除图书
        BookDao bookDao = new BookDao();
        int ret = bookDao.deleteById((int) bookModel.getValueAt(row,0));
        //在ui表中删除
        bookModel.removeRow(row);
        return ret;
    }
    public static int deleteUser(DefaultTableModel userModel, JTable userTable) {
        //获取选中行
        int row = userTable.getSelectedRow();
        if(row==-1) {
            JOptionPane.showMessageDialog(null, "未选择需要删除的行!");
            return -1;
        }
        //根据选中行id在数据库中删除图书
        NormalUserDao normalUserDao = new NormalUserDao();
        int ret = normalUserDao.deleteById((int) userModel.getValueAt(row,0));
        //在ui表中删除
        userModel.removeRow(row);
        return ret;
    }
}
